// require() 导入/加载
const express = require('express')
// 创建程序
const app = express()

// 导入body-parser
const bodyParser = require('body-parser')

// 导入路径模块
const path = require('path')

// 导入cors包
const cors = require('cors')

// 设置资源共享
app.use(cors())

// 解析post请求
app.use(bodyParser.urlencoded({extended:false}))




app.get('/',(req,res)=>{
    res.send('http://127.0.0.1:4000 服务创建成功！')
})
app.get('/test',(req,res)=>{
    let arr = [
        {text:"前端",id:1},
        {text:"后端",id:2},
        {text:"嵌入式",id:3},
    ]
    res.send({
        code:200,
        msg:"测试成功",
        data:arr,
    });
})

app.get('/list',(req,res)=>{
    res.setHeader('Access-Control-Allow-Origin', '*')
    res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
    res.setHeader('Access-Control-Allow-Metheds', 'PUT, POST, GET, DELETE, OPTIONS')
    res.send({
        code:200,
        msg:"测试成功",
        data:[1,2,3,4,5,6],
    })
})


// 路由分发请求
app.use('/api/exam',require('./router/exam.js'))

// 静态资源
app.use('/static',express.static(path.resolve(__dirname,'public')))

const PORT = 4000
app.listen(PORT,()=>{
    console.log("http服务启动......")
    console.log("http://127.0.0.1:"+PORT)
    console.log("http://localhost:"+PORT)
    console.log("http://192.168.0.106:"+PORT)
})